/*********************************************-*- mode:c -*-
 * board_arduino.h
 * this file contains all board specific definitions
 * not the cpu spcific ones!
 *
 * you must define one of the folowing macros to 
 * use this file:
 *
 * Names for special pins or hardware constants:
 *   -  as const static:
 *      #define defConstant(N,V) const static uint8_t N = V;
 *   -  as enum element
 *      #define defConstant(N,V) N = V,
 * defConstants		get all pin fuctions by names
 *
 * pinDef(p,b,t,f,u,i,...)	define a pin by port,bit,timer,function,usage,,pcint...
 *   - all in 1 byte (for m168/m328/m32/m644(p)
 *     #define pinDef(P,B,T,...)	((((P)-1)<<5) | (B) | ((T)<<3))
 *   - all in 2 byte (for m1280/m2560
 *     #define pinDef(P,B,T,...)	((((P)-1)<<8) | (B) | ((T)<<3))
 *   - only port for digital_pin_to_port_PGM[]
 *     #define pinDef(P,...)	(P)
 *   - only bit mask for digital_pin_to_bit_mask_PGM[]
 *     #define pinDef(P,B,...)	_BV(B)
 *   - only timer for digital_pin_to_timer_PGM[]
 *     #define pinDef(P,B,T,...)	(T)
 *
 ***********************************************************
 * Board:	Arduino 1/2009/10000 ... (all with m8/m168/m328)
 * (C) M. Maassen	mic.maassen@gmail.com
 * On the Arduino board, digital pins are also used
 * for the analog output (software PWM).  Analog input
 * pins are a separate set.
 * ATmega with 3 Ports = Arduino */
// ATMEL ATMEGA8 & 168 / ARDUINO
//
//                  +-\/-+
// RST        PC6  1|    |28  PC5 (AI 5)
//      (D 0) PD0  2|    |27  PC4 (AI 4)
//      (D 1) PD1  3|    |26  PC3 (AI 3)
//      (D 2) PD2  4|    |25  PC2 (AI 2)
// PWM+ (D 3) PD3  5|    |24  PC1 (AI 1)
//      (D 4) PD4  6|    |23  PC0 (AI 0)
//            VCC  7|    |22  GND
//            GND  8|    |21  AREF
// XTAL1      PB6  9|    |20  AVCC
// XTAL2      PB7 10|    |19  PB5 (D 13)
// PWM+ (D 5) PD5 11|    |18  PB4 (D 12)
// PWM+ (D 6) PD6 12|    |17  PB3 (D 11) PWM
//      (D 7) PD7 13|    |16  PB2 (D 10) PWM
//      (D 8) PB0 14|    |15  PB1 (D 9) PWM
//                  +----+
//
// (PWM+ indicates the additional PWM pins on the ATmega168.)

/***********************************************************
 * things we want allways 
 ***********************************************************/
#ifndef ARDUINO_X8
// Arduino board with ATmega x8 
#define ARDUINO_X8	0x20150228	/* Date YYYYMMDD */
#endif

#ifdef pinDef
/***********************************************************
 * mapping from pin number to Port,Bit,Timer(PWM),PCInt,...
 ***********************************************************/
/* ATmega with 3 Ports (m8/m168/m328) = Arduino Uno/Duemillanove/... */
/* 0 */
pinDef( D, 0 , NOT_ON_TIMER ,	RXD	, RS232_RxD,16)
pinDef( D, 1 , NOT_ON_TIMER ,	TXD	, RS232_TxD,17)
pinDef( D, 2 , NOT_ON_TIMER ,	INT0	, D2,18)
#ifdef COM2B1
pinDef( D, 3 , TIMER2B	    ,	INT1	, D3,19)
#else
pinDef( D, 3 , NOT_ON_TIMER ,	INT1	, D3,19)
#endif
pinDef( D, 4 , NOT_ON_TIMER ,	T0	, D4,20)
#ifdef COM0B1
pinDef( D, 5 , TIMER0B	    ,	T1	, D5,21)
pinDef( D, 6 , TIMER0A	    ,	AIN0	, D6,22)
#else
pinDef( D, 5 , NOT_ON_TIMER ,	T1	, D5,21)
pinDef( D, 6 , NOT_ON_TIMER ,	AIN0	, D6,22)
#endif
pinDef( D, 7 , NOT_ON_TIMER ,	AIN1	, D7,23)

  /* pin 8 */
pinDef( B, 0 , NOT_ON_TIMER ,	ICP1	, D8,0)
pinDef( B, 1 , TIMER1A      ,	OC1A	, D9,1)
pinDef( B, 2 , TIMER1B      ,	SS	, D10,2)
#ifdef COM21
pinDef( B, 3 , TIMER2	    ,	MOSI	, D11,3)
#else
pinDef( B, 3 , TIMER2A      ,	MOSI	, D11,3)
#endif

pinDef( B, 4 , NOT_ON_TIMER ,	MISO	, D12,4)
pinDef( B, 5 , NOT_ON_TIMER ,	SCK	, LED,5)

  /* pin 14 = A0 */
pinDef( C, 0 , NOT_ON_TIMER ,	ADC0	, A0,8)
pinDef( C, 1 , NOT_ON_TIMER ,	ADC1	, A1,9)
pinDef( C, 2 , NOT_ON_TIMER ,	ADC2	, A2,10)
pinDef( C, 3 , NOT_ON_TIMER ,	ADC3	, A3,11)
pinDef( C, 4 , NOT_ON_TIMER ,	SDA	, A4,12)
pinDef( C, 5 , NOT_ON_TIMER ,	SCL	, A5,13)
#endif /* pinDef */

#ifdef anaDef
/***********************************************************
 * mapping from analog pin to:
 * A*,digPin,adcChannel,
 ***********************************************************/
anaDef( 0, 14, 0)
anaDef( 1, 15, 1)
anaDef( 2, 16, 2)
anaDef( 3, 17, 3)
anaDef( 4, 18, 4)
anaDef( 5, 19, 5)
anaDef( 6, -1, 6)		/* not on digital pin */
anaDef( 7, -1, 7)		/* not on digital pin */
#endif	/* anaDef */

/***********************************************************
 * the following ArduinoDefs_* defines are optional
 * if defined these can lead to faster/smaller code
 ***********************************************************/
#if defined( ArduinoDefs_Digital )
#endif	/* ArduinoDefs_Digital */

#if defined( ArduinoDefs_Analog )
#define analogPinToChannel(P)		(P)
#define analogInputToDigitalPin(p)	(((p) < pins_count_analog) ? (p) + pins_ADC0 : -1)
/* anaDef defines 8 analog inputs for ATmega328 with 32 pins (TQFP) */
/* but ATmega with 28 pins (DIP) have only 6 ananlog inputs */ 
#define NUM_ANALOG_INPUTS           6
#endif

#if defined( ArduinoDefs_PWM )
#if defined(COM0B1) && defined(COM2B1)
#define digitalPinHasPWM(p)         ((p) == 3 || (p) == 5 || (p) == 6 || (p) == 9 || (p) == 10 || (p) == 11)
#else
#define digitalPinHasPWM(p)         ((p) == 9 || (p) == 10 || (p) == 11)
#endif
#endif	/* ArduinoDefs_PWM */

#if defined( ArduinoDefs_PCINT )
#ifdef PCICR
#define digitalPinToPCICR(p)    (((p) >= 0 && (p) <= 21) ? (&PCICR) : ((uint8_t *)0))
#define digitalPinToPCICRbit(p) (((p) <= 7) ? 2 : (((p) <= 13) ? 0 : 1))
#define digitalPinToPCMSK(p)    (((p) <= 7) ? (&PCMSK2) : (((p) <= 13) ? (&PCMSK0) : (((p) <= 21) ? (&PCMSK1) : ((uint8_t *)0))))
#define digitalPinToPCMSKbit(p) (((p) <= 7) ? (p) : (((p) <= 13) ? ((p) - 8) : ((p) - 14)))
#else
/* no PCINT (mega32) */
#define digitalPinToPCICR(p)    ((uint8_t *)0)
#define digitalPinToPCICRbit(p) 0
#define digitalPinToPCMSK(p)    ((uint8_t *)0)
#define digitalPinToPCMSKbit(p) 0
#endif /* PCICR */
#endif	/* ArduinoDefs_PCINT */

#if defined( ArduinoDefs_USB )
#endif	/* ArduinoDefs_USB */
#if defined( ArduinoDefs_Serial )
// These serial port names are intended to allow libraries and architecture-neutral
// sketches to automatically default to the correct port name for a particular type
// of use.  For example, a GPS module would normally connect to SERIAL_PORT_HARDWARE_OPEN,
// the first hardware serial port whose RX/TX pins are not dedicated to another use.
//
// SERIAL_PORT_MONITOR        Port which normally prints to the Arduino Serial Monitor
//
// SERIAL_PORT_USBVIRTUAL     Port which is USB virtual serial
//
// SERIAL_PORT_LINUXBRIDGE    Port which connects to a Linux system via Bridge library
//
// SERIAL_PORT_HARDWARE       Hardware serial port, physical RX & TX pins.
//
// SERIAL_PORT_HARDWARE_OPEN  Hardware serial ports which are open for use.  Their RX & TX
//                            pins are NOT connected to anything by default.

#define SERIAL_PORT_MONITOR         Serial
#define SERIAL_PORT_HARDWARE        Serial
#if defined (UBRR1H)
#define SERIAL_PORT_HARDWARE1       Serial1
#define SERIAL_PORT_HARDWARE_OPEN   Serial1
#endif

#endif /* ArduinoDefs_Serial */

#ifdef defConstant
/***********************************************************
 * define constants for special pins
 ***********************************************************/
#ifndef defClass
#define defClass(C,N) C##_##N
#endif 

#if defined( All_Constants ) || defined( Arduino_Constants )
defConstant(defClass(ARDUINO,LED_BUILTIN), pins_LED)
#endif

defConstant(defClass(_internal_,_avoid_empty_enum_),0)
#endif	/* defConstant */
/***********************************************************
 * clean up Xmacros
 ***********************************************************/
#undef pinDef
#undef defConstant
#undef defClass
#undef All_Constants
